<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

class User extends PC_controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('Usermodel','m_user');
		$this->load->model('Groupmodel','m_group');
	}

	public function index(){
		$data = parent::index();
		$user = $this->session->userdata('user');
		$data['attr']['group_list'] = $this->m_group->outputGroupList4js();
		$data['attr']['group_available_list'] = $user['group_level'] > 0 ? $this->m_group->outputAvailableGroupList4js() : '[]';
		$this->load->view('admin/user',$data);
	}

	public function getUserList(){
		$assist['where'] = getFilter($this->input->post(),Usermodel::$searchField);
		$assist['like'] = getFilter($this->input->post(),Usermodel::$likeField);
		$assist['sort'] = getSort($this->input->post(),Usermodel::$sortField);
		$assist['pagination'] = getPagination($this->input->post());

		list($list,$count) = $this->m_user->getUserListSection($assist);

		$this->response->setSuccess();
		$this->response->setItems($list);
		$this->response->setTotalCount($count);
		$this->response->responseExit();
	}

	public function addUser(){
		$this->validate->setRule('group_id','integer');
		$this->validate->setRule('user_code','string');
		$this->validate->setRule('user_name','string');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();

		if(!$this->m_group->checkGroupLevelAvailable($info['group_id'])) {
			$this->response->setDetail('添加管理员权限超过本人权限!');
			$this->response->responseExit();
		}
		if(!$this->m_user->checkCodeAvailable($info['user_code'])){
			$this->response->setDetail($this->lang->line('error_used'));
			$this->response->responseExit();
		}
		if(!$this->m_user->checkNameAvailable($info['user_name'])){
			$this->response->setDetail($this->lang->line('error_used'));
			$this->response->responseExit();
		}

		$info['user_status'] = STATUS_ACTIVE;
		$info['user_password'] = md5(DEFAULT_PASSWORD.SALT);
		$info['user_time_create'] = NOW;
		if(!$this->m_user->addUser($info)) {
			$this->response->setDetail('手机号码已存在!');
			$this->response->responseExit();
		}
		

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function editUser(){
		$this->validate->setRule('user_id','integer');
		$this->validate->setRule('group_id','integer');
		$this->validate->setRule('user_code','string');
		$this->validate->setRule('user_name','string');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();

		
		if(!$this->m_user->checkCodeAvailable($info['user_code'],$info['user_id'])){
			$this->response->setDetail($this->lang->line('error_used'));
			$this->response->responseExit();
		}
		if(!$this->m_user->checkNameAvailable($info['user_name'],$info['user_id'])){
			$this->response->setDetail($this->lang->line('error_used'));
			$this->response->responseExit();
		}

		if(!$this->m_user->editUser($info)) {
			$this->response->setDetail('手机号码已存在!');
			$this->response->responseExit();
		}
		

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function resetPassword(){
		$this->validate->setRule('user_id','integer');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();
		$info['user_password'] = md5(DEFAULT_PASSWORD.SALT);
		
		$this->m_user->editUser($info);
		

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function disableUser(){
		$this->validate->setRule('user_id','integer');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();
		$info['user_status'] = STATUS_DISABLE;
		
		$this->m_user->editUser($info);
		

		$this->response->setSuccess();
		$this->response->responseExit();
	}

	public function activeUser(){
		$this->validate->setRule('user_id','integer');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();
		$info['user_status'] = STATUS_ACTIVE;
		
		$this->m_user->editUser($info);
		

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function importUser() {
		$this->validate->setRule('file','file');
        $this->validate->setRule('file','message',$this->lang->line('error_parameter'));
        if(!$this->validate->run($this->input->post())){
            $this->response->setDetail($this->validate->getMessage());
            $this->response->responseExit();
        }
        $info = $this->validate->getResult();

        $this->load->library('zmexcel');
        $this->zmexcel->checkExcelFile($info['file']);
        $path = $this->m_app->saveFile($info['file'],array(),UPLOAD_PATH_FILE);
        $this->zmexcel->init(UPLOAD_PATH_FILE.$path);

        @set_time_limit(0);
        @ini_set('memory_limit', '2048M');
        $data = $this->zmexcel->getDatas();

        $this->load->database();
        $assist = array();
        list($list,$count) = $this->m_user->getUserListSection($assist);
        $list = reindexArray($list,'user_code');

        $insert_arr = array();
        $update_arr = array();

        $existed = array();
        foreach ($data as $k => $record) {
            foreach($record as $key => $value) {
                if(!$value || $value==null) $record[$key] = '';
                $record[$key] = trim($record[$key]);
            }
            $k += 2;
            $user_code = $record['C'];
            if($user_code == '' || isset($existed[$user_code])) continue;
            $data = array(
            	'user_code' => $user_code,
            	'user_name' => $record['D'],
            	'user_province' => $record['A'],
            	'user_city' => $record['B'],
            	'shop_address' => $record['E'],
            	'shop_phone' => $record['F'],
            	'shop_support_chezhu' => $record['G'] == '是' ? 1 : 0,
            	'shop_support_shuangkuai' => $record['H'] == '是' ? 1 : 0,
            );
            if(isset($list[$user_code])) {
            	if($list[$user_code]['group_id'] != 2) {
            		$this->response->setDetail("第{$k}行编码[{$user_code}]与管理员重复!!!");
        			$this->response->responseExit();
            	}
                $update_arr[] = $data;
            } else {
            	$data['user_time_create'] = NOW;
            	$data['user_status'] = STATUS_ACTIVE;
            	$data['group_id'] = 2;
            	$data['user_password'] = md5(strtolower('888888').SALT);
            	$insert_arr[] = $data;
            }
            $existed[$user_code] = 1;
        }
        $this->m_user->createUserBatch($insert_arr);
        $this->m_user->updateUserBatch($update_arr);
        $this->db->close();

		$this->load->driver('cache', array('adapter' => 'file'));
        @$this->cache->delete('shop_list.cache');

        $this->response->setSuccess();
        $this->response->setDetail($this->lang->line('success_update'));
        $this->response->responseExit();
	}
}
/* End of file user.php */
/* Location: ./application/controllers/user.php */